package com.hr.leetcode.哈希表;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 周恒哲
 * @date 2020/05/15
 **/
public class Top1_两数之和 {
    //暴力法
    public static int[] twoSum(int[] nums, int target) {
        int[] res=new int[2];
        for (int i=0;i<nums.length;i++){
            for (int j=i+1;j<nums.length;j++){
                if (nums[i]+nums[j]==target){
                    res[0]=i;
                    res[1]=j;

                }
            }
        }
        return res;
    }
    public static int[] twoSum1(int[] nums, int target) {
        Map<Integer,Integer> map=new HashMap<>();
        int[] res=new int[2];
        for (int i=0;i<nums.length;i++){
            map.put(nums[i],i);
        }
        for (int j=0;j<nums.length;j++){
            int tmp=target-nums[j];
            if (map.containsKey(tmp)&&map.get(tmp)!=j){
                res[0]=map.get(tmp);
                res[1]=j;
            }
        }
        return res;
    }
}